package org.example.dingtalkintegrationservice.dingtalk.processTask;
import com.alibaba.fastjson.JSON;
import com.aliyun.dingtalkworkflow_1_0.Client;
import com.aliyun.dingtalkworkflow_1_0.models.CreateIntegratedTaskHeaders;
import com.aliyun.dingtalkworkflow_1_0.models.CreateIntegratedTaskRequest;
import com.aliyun.dingtalkworkflow_1_0.models.CreateIntegratedTaskResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import jakarta.annotation.Resource;
import org.example.dingtalkintegrationservice.exception.DingTalkCallExceptionGroovy;
import org.example.dingtalkintegrationservice.exception.WrongParamExceptionGroovy;
import org.example.dingtalkintegrationservice.pojo.IdRecord;
import org.example.dingtalkintegrationservice.repository.IdRecordRepository;
import org.example.dingtalkintegrationservice.service.IdRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Collections;

/**
 * @author pc
 */
@Component
public class CreateProcessTask {
@Autowired
IdRecordService idRecordService;
@Resource
    IdRecordRepository idRecordRepository;
    static Client createClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new Client(config);
    }
    static CreateIntegratedTaskResponse createTask  (String token, String userId, String processInstanceId, String activityId) throws Exception {
        if (!activityId.startsWith("T")){
            throw new WrongParamExceptionGroovy("activity",activityId);
        }
        Client client = createClient();
        CreateIntegratedTaskHeaders createIntegratedTaskHeaders = new CreateIntegratedTaskHeaders();
        createIntegratedTaskHeaders.xAcsDingtalkAccessToken = token;
        CreateIntegratedTaskRequest.CreateIntegratedTaskRequestTasks tasks0 = new CreateIntegratedTaskRequest.CreateIntegratedTaskRequestTasks()
                .setUserId(userId)
//    .setUrl("http://991s3t5639.zicp.fun/getTaskId?taskId=${activityId}")
                .setUrl("https://www.dingtalk.com")
                .setCustomData("customData1219");
        CreateIntegratedTaskRequest createIntegratedTaskRequest = new CreateIntegratedTaskRequest()
                .setProcessInstanceId(processInstanceId)
                .setActivityId(activityId)
                .setTasks(Collections.singletonList(
                        tasks0
                ));
        var res=client.createIntegratedTaskWithOptions(createIntegratedTaskRequest, createIntegratedTaskHeaders, new RuntimeOptions());
        if (res.statusCode!=200){
            throw new DingTalkCallExceptionGroovy(res.body.toString());
        }
        return  res;
    }
public String exec(String token,String userId,String flowId,String activityId) throws Exception {
        String processInstanceId=idRecordService.getId2ById1(flowId);
        var body=createTask(token, userId, processInstanceId, activityId).body;
        var output=JSON.toJSONString(body);
        var taskId= body.result.getFirst().taskId;
        IdRecord idRecord=new IdRecord();
    idRecord.setId1(activityId);
    idRecord.setId2(String.valueOf(taskId));
    idRecord.setParentId(processInstanceId);
    idRecord.setOperate("create");
    idRecord.setUserId(userId);
    idRecord.setTag("/createProcessTask");
    idRecordRepository.save(idRecord);
    return output;
}

}
